package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {

        this.save(setmealDto);
        //获取口味list集合
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //stream字符流lam遍历并转换为集合输出
        setmealDishes.stream().map(list -> {
            list.setSetmealId(setmealDto.getId());
            return list;
        }).collect(Collectors.toList());
        //setmealDishservice调用save方法
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 批量修改状态停售和起售
     *
     * @param status
     * @param ids
     */
    @Override
    public void updataStatus(int status, List<Long> ids) {
        //查看状态
        log.info("状态:{}", status);
        //lambda进行条件构造
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //条件in(ids)
        queryWrapper.in(Setmeal::getId, ids);
        //new一个setmeal将status传入
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        this.update(setmeal, queryWrapper);
    }
    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        //先删除套餐表中的数据
        this.removeByIds(ids);
       //构造条件
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据
        setmealDishService.remove(lambdaQueryWrapper);
    }

    /**
     * 修改套餐
     * 先删再加
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithSetmealName(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        //清理当前套餐对应菜品数据
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(lqw);

        //添加当前提交过来的套餐菜品数据
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes.stream().map(setmealDishe -> {
            setmealDishe.setSetmealId(setmealDto.getId());
            return setmealDishe;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public SetmealDto getByIdWithSetmealName(Long id) {
        //查询菜品基本信息，从dish表查询

        Setmeal setmeal = this.getById(id);


        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前菜品对应的菜品分类信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }
}


